package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {


    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<CourseDTO> getCourses(CourseDTO courseDto) {
        PageHelper.startPage(courseDto.getPage(), courseDto.getPageSize());
        List<CourseDTO> list = this.baseMapper.getCourses(courseDto);
        Page<CourseDTO> page = (Page<CourseDTO>) list;
        return PageResult.<CourseDTO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }


    @Override
    public void deleteCourse(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void addCourse(CourseDTO courseDto) {
        Course course = BeanUtil.copyProperties(courseDto, Course.class);
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        this.save( course);
    }

    @Override
    public Course getCourseById(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void updateCourse(CourseDTO courseDto) {
        Course course = BeanUtil.copyProperties(courseDto, Course.class);
        course.setUpdateTime(LocalDateTime.now());
        this.updateById(course);
    }

    @Override
    public List<Course> listCourses() {
        return courseMapper.selectAll();
    }
}
